package cn.edu.scau.cmi.chenyipeng.spring.gui;

import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.chenyipeng.spring.dao.StudentDAO;
import cn.edu.scau.cmi.chenyipeng.spring.dao.TeacherDAO;
import cn.edu.scau.cmi.chenyipeng.spring.domain.Student;
import cn.edu.scau.cmi.chenyipeng.spring.domain.Teacher;
import cn.edu.scau.cmi.chenyipeng.spring.util.ComprehensionApplicationContextUtil;
import cn.edu.scau.cmi.chenyipeng.spring.util.ScauCmiHibernateSessionFactoryUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.event.*;

public class SpringController {

	@FXML
	private ResourceBundle resources;

	@FXML
	private URL location;

	@FXML
	private Button deleteSB;

	@FXML
	private Button deleteTB;

	@FXML
	private TextField sName;

	@FXML
	private Button sChooseTB;

	@FXML
	private Button sAlterTB;

	@FXML
	private Button addTB;

	@FXML
	private Button tAlterSB;

	@FXML
	private TextField tID;

	@FXML
	private TextField sID;

	@FXML
	private Button tChooseSB;

	@FXML
	private Button addSB;

	@FXML
	private TextField tName;

	@FXML
	private TextField sTeacher;

	@FXML
	private Button alterTB;

	@FXML
	private Button alterSB;
	
	@FXML
    private Button update;
	
	@FXML
    private Label teacherView;
	
	@FXML
    private Label studentView;

	@FXML
	void addS(ActionEvent event) {
		Student s = new Student();
		s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDAO");
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.save(s);
		t.commit();
		session.close();
	}

	@FXML
	void alterS(ActionEvent event) {
		Student s = new Student();
		s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDAO");
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.update(s);
		t.commit();
		session.close();
	}

	@FXML
	void deleteS(ActionEvent event) {
		Student s = new Student();
		s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDAO");
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.delete(s);
		t.commit();
		session.close();
	}

	@FXML
	void sChooseT(ActionEvent event) {
		Student s = new Student();
		Teacher tea = new Teacher();
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDAO");
		s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		tea.setStudent(s);
		s.setTeacher(tea);
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.update(s);
		t.commit();
		session.close();
	}

	@FXML
	void sAlterT(ActionEvent event) {
		Student s = new Student();
		Teacher tea = new Teacher();
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDAO");
		s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		tea.setStudent(s);
		s.setTeacher(tea);
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.update(s);
		t.commit();
		session.close();
	}

	@FXML
	void addT(ActionEvent event) {
		Teacher tea = new Teacher();
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		TeacherDAO tDAO = (TeacherDAO) ac.getBean("teacherDAO");
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		tDAO.save(tea);
		t.commit();
		session.close();
	}

	@FXML
	void alterT(ActionEvent event) {
		Teacher tea = new Teacher();
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		TeacherDAO tDAO = (TeacherDAO) ac.getBean("teacherDAO");
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		tDAO.update(tea);
		t.commit();
		session.close();
	}

	@FXML
	void deleteT(ActionEvent event) {
		Teacher tea = new Teacher();
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		TeacherDAO tDAO = (TeacherDAO) ac.getBean("teacherDAO");
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		tDAO.delete(tea);
		t.commit();
		session.close();
	}

	@FXML
	void tChooseS(ActionEvent event) {
		Student s = new Student();
		Teacher tea = new Teacher();
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDAO");
		s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		tea.setStudent(s);
		s.setTeacher(tea);
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.update(s);
		t.commit();
		session.close();
	}

	@FXML
	void tAlterS(ActionEvent event) {
		Student s = new Student();
		Teacher tea = new Teacher();
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDAO");
		s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		tea.setStudent(s);
		s.setTeacher(tea);
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.update(s);
		t.commit();
		session.close();
	}

	@FXML
    void updateView(ActionEvent event) {
		studentView.setText(getAllStudent());
		teacherView.setText(getAllTeacher());;
    }
	
	String getAllStudent() {
    	String out = String.format("%-12s %-12s %-12s\n", "ID",	"name", "tutor");
    	Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
    	//StudentDao studentDao = new StudentDao();
    	ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO studentDao=(StudentDAO) ac.getBean("studentDAO");
		
    	List students = studentDao.findAll();
		Iterator<?> studentIterator = students.iterator();
		while(studentIterator.hasNext()){
			Student student = (Student) studentIterator.next();
			if(student.getTeacher()==null) {
				out = out + String.format("%-12d %-12s %-12s\n", student.getId(), student.getName() , student.getTeacher());
				//out = out + student.getId()+"		" + student.getName() + "		" + student.getTeacher() + "\n";
			}
			else {
				out = out + String.format("%-12d %-12s %-12d\n", student.getId(), student.getName() , student.getTeacher().getId());
				//out = out + student.getId().toString() + "		" + student.getName() + "		" + student.getTeacher().getId() + "\n";
			}
		}
		transaction.commit();
		session.close();
		return out;
    }
    
    String getAllTeacher() {
    	String out = String.format("%-12s %-12s\n", "ID",	"name");
    	Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		//TeacherDao teacherDao = new TeacherDao();
		ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		TeacherDAO teacherDao=(TeacherDAO) ac.getBean("teacherDAO");
		
    	List teachers = teacherDao.findAll();
		Iterator<?> teacherIterator = teachers.iterator();
		while(teacherIterator.hasNext()){
			Teacher teacher = (Teacher) teacherIterator.next();
			out = out + String.format("%-12d %-12s\n", teacher.getId(), teacher.getName());
		}
		transaction.commit();
		session.close();
		return out;
    }
	
	@FXML
	void initialize() {
		assert deleteSB != null : "fx:id=\"deleteSB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert deleteTB != null : "fx:id=\"deleteTB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert sName != null : "fx:id=\"sName\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert sChooseTB != null : "fx:id=\"sChooseTB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert sAlterTB != null : "fx:id=\"sAlterTB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert addTB != null : "fx:id=\"addTB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert tAlterSB != null : "fx:id=\"tAlterSB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert tID != null : "fx:id=\"tID\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert sID != null : "fx:id=\"sID\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert tChooseSB != null : "fx:id=\"tChooseSB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert addSB != null : "fx:id=\"addSB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert tName != null : "fx:id=\"tName\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert sTeacher != null : "fx:id=\"sTeacher\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert alterTB != null : "fx:id=\"alterTB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
		assert alterSB != null : "fx:id=\"alterSB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";

	}
}
